//
//  BannerView.m
//  BannerDemo
//
//  Created by 边果互动 on 2021/3/10.
//

#import "BannerView.h"
#import <SDWebImage/SDWebImage.h>

@interface BannerView() <UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControlView;
@property (nonatomic, strong) NSTimer *timer;

@end

@implementation BannerView

+ (BannerView *) bannerWithUrls: (NSMutableArray *) imgUrls {
   
    BannerView *bannerView = [[[NSBundle mainBundle] loadNibNamed:@"BannerView" owner:nil options:nil] firstObject];
    bannerView.frame = CGRectMake(0, 0, ScreenWidth, 200);
    [bannerView setImgUrls:imgUrls];
    return bannerView;
}

-(void) setImgUrls:(NSArray *)imgUrls {
    _imgUrls = imgUrls;
    for (UIView *view in [self.scrollView subviews]) {
        [view removeFromSuperview];
    }
    NSInteger size = imgUrls.count;
    CGFloat imgW = self.frame.size.width;
    CGFloat imgH = self.frame.size.height;
    CGFloat imgY = 0;
    for (int i = 0; i < size; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        CGFloat imgX = i * imgW;
        imageView.frame = CGRectMake(imgX, imgY, imgW, imgH);
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        NSString *imgUrl = imgUrls[i];
        [imageView sd_setImageWithURL:[NSURL URLWithString:imgUrl]];
        [self.scrollView addSubview:imageView];
    }
    self.scrollView.frame = self.frame;
    //设置contentSize才能让ScrollView可以滑动
    //只有当contentSize的宽度大于frameSize的宽度的时候才能左右滑动
    //只有当contentSize的高度大于frameSize的高度的时候才能上下滑动
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * size, 0);
    //添加分页效果
    [self.scrollView setPagingEnabled:YES];
    //去掉横向滑动的滚动条
    self.scrollView.showsHorizontalScrollIndicator = NO;
    //给ScrollView设置代理，用来监听滑动
    self.scrollView.delegate = self;
    
    //添加小圆点
    //设置总共小圆点个数
    self.pageControlView.numberOfPages = size;
    //设置当前的小圆点
    self.pageControlView.currentPage = 0;
    
    [self startTimer];
}

//启动计时器
-(void) startTimer {
    if (nil == self.timer) {
        //创建一个计时器
        //参数一：每隔多少s执行一次
        //参数二：执行哪个对象
        //参数三：执行哪个方法
        //参数四：
        //参数五：是否重复执行
        self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(doTimer) userInfo:nil repeats:YES];
        
        NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
        [runLoop addTimer:self.timer forMode:NSRunLoopCommonModes];
    }
}

//关闭计时器
-(void) stopTimer {
    if (nil != self.timer) {
        //停止计时器，这个时候计时器就废了
        //下次再启动需要重新开启一个计时器
        [self.timer invalidate];
        self.timer = nil;
    }
}

//计时器执行的内容
-(void) doTimer {
    NSInteger currentPage = self.pageControlView.currentPage;
    currentPage = (currentPage + 1) % (self.pageControlView.numberOfPages);
    CGFloat offsetX = currentPage * self.scrollView.frame.size.width;
    [self.scrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}

/**
 * ScrollView开始被拖拽的时候调用
 */
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self stopTimer];
}

/**
 * scrollView正在滑动的时候被调用
 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //如何计算当前滚到了第几页
    //1. 获取滚动x方向的偏移量
    CGFloat offsetX = scrollView.contentOffset.x;
    //设置这一步可以让页面滚动到一半的时候就改变小圆点的颜色
    offsetX = offsetX + (scrollView.frame.size.width / 1.5);
    //2. 用x方向的偏移量 / 每一页的宽度 = 当前第几页
    int page = offsetX / scrollView.frame.size.width;
    self.pageControlView.currentPage = page;
}

/**
 * ScrollView结束拖拽的时候调用
 */
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [self startTimer];
}
@end
